package controller

import (
	"github.com/gin-gonic/gin"
	"hytx_manager/models"
	"hytx_manager/pkg/app"
	"net/http"
	"github.com/Unknwon/com"
	"github.com/astaxie/beego/validation"
	"hytx_manager/pkg/e"
	"time"
)

// 树形 权限
func GetPermissions(c *gin.Context) {
	appG := app.Gin{c}
	data := models.GetPermissionAll()

	appG.ResponseMsg(http.StatusOK, "ok", data)

}

func GetPermissionById(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.Query("id")).MustInt()
	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于1！")
	data := models.Permission{ID: id}
	permission, err := data.GetPermissionById()
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "查找成功！", permission)

}

// 添加权限
func AddPermission(c *gin.Context) {
	appG := app.Gin{c}
	//parent_id
	parent_id := com.StrTo(c.PostForm("parent_id")).MustInt()
	name := c.PostForm("name")
	display_name := c.PostForm("display_name")
	api_url := c.PostForm("api_url")
	front_url := c.PostForm("front_url")
	description := c.PostForm("description")
	action := c.PostForm("action")
	method := c.PostForm("method")
	icon := c.PostForm("icon")
	component := c.PostForm("component")
	is_menu := com.StrTo(c.PostForm("is_menu")).MustInt()
	sort := com.StrTo(c.PostForm("sort")).MustInt()

	valid := validation.Validation{}
	valid.Required(name, "name").Message("姓名不能为空！")
	valid.Required(display_name, "display_name").Message("前台显示名称不能为空！")
	//valid.Required(api_url, "api_url").Message("接口地址不能为空！")
	//valid.Required(front_url, "front_url").Message("前台地址不能为空！")
	data := models.Permission{
		ParentId:    parent_id,
		Name:        name,
		DisplayName: display_name,
		ApiUrl:      api_url,
		FrontUrl:    front_url,
		Description: description,
		Action:      action,
		Method:      method,
		Icon:        icon,
		IsMenu:      is_menu,
		Component:   component,
		CreatedAt:   time.Now(),
		Sort:        sort,
	}
	if err := data.AddPermission(); err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "添加成功！", "")
}

// 修改权限

func UpdatePermission(c *gin.Context) {
	appG := app.Gin{c}
	//parent_id
	id := com.StrTo(c.Query("id")).MustInt()
	parent_id := com.StrTo(c.PostForm("parent_id")).MustInt()
	name := c.PostForm("name")
	display_name := c.PostForm("display_name")
	api_url := c.PostForm("api_url")
	front_url := c.PostForm("front_url")
	description := c.PostForm("description")
	action := c.PostForm("action")
	method := c.PostForm("method")
	icon := c.PostForm("icon")
	component := c.PostForm("component")
	is_menu := com.StrTo(c.PostForm("is_menu")).MustInt()
	sort := com.StrTo(c.PostForm("sort")).MustInt()

	valid := validation.Validation{}
	valid.Min(id, 1, "id").Message("id必须大于1！")
	valid.Required(name, "name").Message("姓名不能为空！")
	valid.Required(display_name, "display_name").Message("前台显示名称不能为空！")
	valid.Required(api_url, "api_url").Message("接口地址不能为空！")
	valid.Required(front_url, "front_url").Message("前台地址不能为空！")
	data := models.Permission{
		ID:          id,
		ParentId:    parent_id,
		Name:        name,
		DisplayName: display_name,
		ApiUrl:      api_url,
		FrontUrl:    front_url,
		Description: description,
		Action:      action,
		Method:      method,
		Icon:        icon,
		IsMenu:      is_menu,
		Component:   component,
		UpdatedAt:   time.Now(),
		Sort:        sort,
	}
	if err := data.UpdatePermission(); err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "修改成功！", "")
}

// 删除权限

func DeletedPermission(c *gin.Context) {
	appG := app.Gin{c}
	id := com.StrTo(c.PostForm("id")).MustInt()

	err := models.DeletedPermission(id)
	if err != nil {
		appG.Response(http.StatusOK, e.ERROR, err)
	}
	appG.ResponseMsg(200, "删除成功！", "")

}
